<%@ Language="VBScript" %>
<!-- #include file="aspuploader/include_aspuploader.asp" -->
<!-- #include file="adovbs.inc" -->
<%

Dim uploader,mvcfile
Set uploader=new AspUploader

Set objConn = Server.CreateObject("ADODB.Connection")
'objConn.ConnectionString = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("Developer.mdb")
objConn.ConnectionString = "dsn=Developer;uid=JohnNgo;pwd=jo2.0G$!;"    
objConn.Open


Function ToTwoChar(str)
	If Len(str)>1 Then
		ToTwoChar=str
	Else
		ToTwoChar="0" & str
	End If
End Function

Function GetNewDateTime()
	GetNewDateTime=Year(Date()) & ToTwoChar(Month(Date())) & ToTwoChar(Day(Date())) & ToTwoChar(Hour(Time())) & ToTwoChar(Minute(Time())) & ToTwoChar(Second(Time()))
End Function

If Request.QueryString("download")&""<>"" Then
	'Set mvcfile=uploader.GetUploadedFile(Request.QueryString("download"))
	list2=Split(Request.Form("delete"),"/")
	
	s_FilePathAndName = Server.MapPath("Photos/" & list1(0) & "/" & list1(1))
	
	Response.ContentType="application/oct-stream"
	Response.AddHeader "Content-Disposition","attachment; filename=""" & s_FilePathAndName & """"
	
	Dim data,stream
	Set stream=CreateObject("ADODB.Stream")
	stream.Mode=3
	stream.Type=1
	stream.Open()
	stream.LoadFromFile(s_FilePathAndName)
	Dim ws,cs
	ws=0
	while ws<stream.Size
		cs=stream.Size-ws
		If cs>1000 Then
			cs=1000
		End If
		data=stream.Read(cs)
		Response.BinaryWrite(data)
		Response.Flush()
		ws=ws+cs
	wend
	
	stream.Close()
	Response.End()
End If

If Request.Form("delete")&""<>"" Then
	Set mvcfile=uploader.GetUploadedFile(Request.Form("delete"))
	Dim fso
	Set fso=CreateObject("Scripting.FileSystemObject")
	fso.DeleteFile(mvcfile.FilePath)
	Response.Write("OK")
	Response.End()
End If



If Request.Form("guidlist")&""<>"" Then
	Dim list,i
	list=Split(Request.Form("guidlist"),"/")
	Response.Write("[")
	For i=0 to Ubound(list)
		if i>0 then
			Response.Write(",")
		end if
		Set mvcfile=uploader.GetUploadedFile(list(i))
		Response.Write("{")
		Response.Write("FileGuid:'" & mvcfile.FileGuid & "'")
		Response.Write(",")
		Response.Write("FileSize:'" & mvcfile.FileSize & "'")
		Response.Write(",")
		Response.Write("FileName:'" & mvcfile.FileName & "'")
		Response.Write("}")
	Next
	Response.Write("]")
	
End If


If Request.Form("AddFilesToDB")&""<>"" Then
On Error Resume Next
        Dim list1, j, a_mvcfile, m_file, destfilename, firstFilename, count
        count = 0
        '0:username and 1: albumID
    	list1=Split(Request.Form("AddFilesToDB"),"/")

        a_mvcfile=Split(list1(2),"$")

    	Set fso=CreateObject("Scripting.FileSystemObject")

        savefolder=Server.MapPath("Photos\" + list1(0))

    	strSQL = "SELECT * FROM photos"
        set objRSPhotos = Server.CreateObject("ADODB.Recordset")
        objRSPhotos.Open strSQL, objConn, adOpenKeyset, adLockOptimistic, adCmdText

    	For Each m_file In a_mvcfile
            Set mvcfile=uploader.GetUploadedFile(m_file)
    		If fso.FileExists(savefolder & "\" & mvcfile.FileName) = True Then 
    		 fntemp=Split(mvcfile.FileName,".")
    		 destfilename = fntemp(0) & "_" & GetNewDateTime() & "." & fntemp(1)
    		Else
    		 destfilename = mvcfile.FileName
    	    End if 
            
            If count = 0 Then
                firstFilename = destfilename
                count = a_mvcfile.count
            End if
            
    		fso.MoveFile mvcfile.FilePath,savefolder & "\" & destfilename
            
            ' Edited at 05/12/2010
            ' Creating Thumbnails and Resizing photos 
            ' Ex : Call CreateThumb("John","My Photo.jpg")
            'Application.Lock
            Call CreateThumb(list1(0),destfilename)
            'Application.Unlock 

    		objRSPhotos.AddNew
    		objRSPhotos.Fields("username").Value      = list1(0)
    		objRSPhotos.Fields("filename").Value   = destfilename
    		objRSPhotos.Fields("albumID").Value   = CInt(list1(1))
    		objRSPhotos.Fields("priority").Value   = 1
    		objRSPhotos.Fields("dateTaken").Value   = Date
            objRSPhotos.Update

    		
    	Next
		Set fso = Nothing
		objRSPhotos.Close
		set objRSPhotos = Nothing
	Response.Write("/Photos/" + list1(0)+"/Thumbnails/"+firstFilename)
	Response.End()
End If


If Request.Form("OnChange")&""<>"" Then
	i=0
	info1=Split(Request.Form("OnChange"),"/")
	strSQL = "SELECT * FROM photos WHERE albumID = " & info1(1) & " AND username = '" & info1(0) & "'"
	Set objRSPhotos = Server.CreateObject("ADODB.Recordset")
	objRSPhotos.Open strSQL, objConn
	    If Not objRSPhotos.EOF then
			Response.Write("[")
			Do While Not objRSPhotos.EOF
				if i>0 then
					Response.Write(",")
				end if
				Response.Write("{")
				Response.Write("FileGuid:'" & objRSPhotos("photoID") & "'")
				Response.Write(",")
				Response.Write("FileSize:'" & "0" & "'")
				Response.Write(",")
				Response.Write("FileName:'" & objRSPhotos("fileName") & "'")
				Response.Write("}")
				
				i=i+1
				objRSPhotos.MoveNext
			Loop	
			Response.Write("]")
		Else 
		Response.Write("nothing")
 		End If
    	objRSPhotos.Close
	    set objRSPhotos = Nothing
        
    	objConn.Close
	    set objConn = Nothing
 
End If

If Request.Form("ShowPhotos")&""<>"" Then
	i=0
	info1=Split(Request.Form("ShowPhotos"),"/")


	strSQL = "SELECT * FROM photos WHERE albumID = " & info1(1) & " AND username = '" & info1(0) & "'"
	Set objRSPhotos = Server.CreateObject("ADODB.Recordset")
	objRSPhotos.Open strSQL, objConn
	    If Not objRSPhotos.EOF then
			Response.Write("[")
			Do While Not objRSPhotos.EOF
				if i>0 then
					Response.Write(",")
				end if
				Response.Write("{")
				Response.Write("FileGuid:'" & objRSPhotos("photoID") & "'")
				Response.Write(",")
				Response.Write("FileSize:'" & "0" & "'")
				Response.Write(",")
				Response.Write("FileName:'" & objRSPhotos("fileName") & "'")
				Response.Write("}")
				
				i=i+1
				objRSPhotos.MoveNext
			Loop	
 		End If
    	objRSPhotos.Close
	    set objRSPhotos = Nothing
        
    	objConn.Close
	    set objConn = Nothing
       Response.Write("]")
    
End If

If Request.Form("addPhoto")&""<>"" Then

	Dim info
	info=Split(Request.Form("addPhoto"),"/")
	
	' some default values.
	strSQL = "SELECT * FROM photos"
	set objRSPhotos = Server.CreateObject("ADODB.Recordset")
	objRSPhotos.Open strSQL, objConn, adOpenKeyset, adLockOptimistic, adCmdText
	
		objRSPhotos.AddNew

		objRSPhotos.Fields("username").Value      = info(0)
		objRSPhotos.Fields("filename").Value   = info(1)
		objRSPhotos.Fields("albumID").Value   = CInt(info(2))
		objRSPhotos.Fields("priority").Value   = 1
		objRSPhotos.Fields("dateTaken").Value   = Date

		objRSPhotos.Update
		objRSPhotos.Close
		set objRSPhotos = Nothing

	Response.End()
End If

If Request.Form("showDropDownList")&""<>"" Then
	i=0
	s_Uname=Request.Form("showDropDownList")
  
	   '-- SQL Statement
	strSQL = "SELECT * FROM photoAlbums WHERE username = '" & Trim(s_Uname) & "' ORDER BY albumName ASC"
	'-- Execute our SQL statement and store the recordset
	Set objRSPhotos = objConn.Execute(strSQL)
	'-- START MAIN CODE BLOCK
	'-- If we have records to return
	if not objRSPhotos.EOF then
	'-- Open a form/select tag
	'-- This particular example has no action attribute, which would normally...
	'-- ...be required in order to post the data to another page for processing.
	'Response.Write "<form name=""form1"">"
	Response.Write "<select name=""dropdownlist"" onchange='OnChange(this);'>" & vbCrLf
	Response.Write "<option selected=""selected"" value=-1>Add New Album...</option>"
	
	'-- loop and build each database entry as a selectable option
	While objRSPhotos.EOF = false
	
	Response.Write "<option value='"& objRSPhotos.Fields("albumID").Value & "'>" & objRSPhotos.Fields("albumName").Value & "</option>" & vbCrLf
	'-- Move recordset to the next value
	objRSPhotos.movenext
	Wend
	end if
	'--END OF MAIN CODE BLOCK
	
	'-- close select/form tags
	Response.Write "</select>" & vbCrLf
	Response.End()
	objRSPhotos.Close
	set objRSPhotos = Nothing
End If

'-------------------------------------------------------------
' Create       : 04/12/2010 NP
' Description  : Create Thumbnails. 
'                If the photo has size > 1000 then resizing it    
'-------------------------------------------------------------
Sub CreateThumb(username,fileName)

	Dim objShell, strThumbCommand, strResizeCommand, strCommandGetSize, strSize, objScriptExec
	Dim maxSize, savePath, saveThumbPath, demension, thumbSize, strCommand, objExec
	Set objShell = Server.CreateObject("WScript.Shell")
	savePath = Server.MapPath("/Photos/" & username & "/")
	saveThumbPath = savePath & "\Thumbnails"
	thumbSize = "100x100"
	maxSize = "800x600"
	strThumbCommand = Server.MapPath("/aspuploader/convert") &_
	                 " -sample " & thumbSize &_
	                 " " & chr(34) & savePath & "\" & fileName & chr(34) &_
	                 " " & chr(34) & saveThumbPath & "\" & fileName & chr(34)
	strResizeCommand = Server.MapPath("/aspuploader/convert") &_
	                 " -sample "&maxSize&_
	                 " " & chr(34) & savePath & "\" & fileName & chr(34) &_
	                 " " & chr(34) & savePath & "\" & fileName & chr(34)                
    strCommandGetSize = Server.MapPath("/aspuploader/identify") &_
                        " -format '%wx%h' " &_
                        chr(34) & savePath & "\" & fileName & chr(34)
    
    'If the folder does not already exist then create the folder
    Dim myFSO
    Set myFSO = Server.CreateObject("Scripting.FileSystemObject")
    If NOT myFSO.FolderExists(saveThumbPath) Then
        myFSO.CreateFolder(saveThumbPath)
    End If    
    Set myFSO = NOTHING 
   
   	'Create Thumb
    objShell.Exec(strThumbCommand)
   
    'Get demension 
	Set objScriptExec = objShell.Exec(strCommandGetSize)
    strSize = objScriptExec.StdOut.ReadAll
    strSize = Replace(strSize,"'","")
    demension = Split(strSize,"x")
    
    'Resize if width or heigh > 1000
    If CInt(demension(0)) > 1000 OR CInt(demension(1)) > 1000 Then 
        objShell.Exec(strResizeCommand)
    End If
 
	Set objShell=Nothing
End Sub

Response.End()

%>
